/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Schnittstellen.Audio;

import Schnittstellen.RTSObject;
import java.util.LinkedList;
import java.util.Random;
import java.util.Vector;

/**
 *
 * @author Geez
 */
public class Soundset extends RTSObject {

    public static final int WHAT = 0;
    public static final int CONFIRM = 1;
    public static final int ATTACK = 2;
    public static final int DEATH = 3;
    public static final int BIRTH = 4;
    public static final int PISSED = 5;
    public static final int NOTINUSE = -1;
    private LinkedList<Sound> what;
    private LinkedList<Sound> confirm;
    private LinkedList<Sound> attack;
    private LinkedList<Sound> death;
    private LinkedList<Sound> birth;
    private LinkedList<Sound> pissed;
    private Vector<Sound> all;
    private Random r;
    private String name;

    public Soundset() {
        what = new LinkedList<Sound>();
        confirm = new LinkedList<Sound>();
        attack = new LinkedList<Sound>();
        death = new LinkedList<Sound>();
        birth = new LinkedList<Sound>();
        pissed = new LinkedList<Sound>();
        all = new Vector<Sound>();
    }

    public Soundset(String name) {
        this();
        this.name = name;
        r = new Random();
    }

    public boolean addSound(int typ, Sound sound) {
        if (!all.contains(sound)) {
            all.add(sound);
            switch (typ) {
                case WHAT: {
                    return what.add(sound);
                }
                case CONFIRM: {
                    return confirm.add(sound);
                }
                case ATTACK: {
                    return attack.add(sound);
                }
                case DEATH: {
                    return death.add(sound);
                }
                case BIRTH: {
                    return birth.add(sound);
                }
                case PISSED: {
                    return pissed.add(sound);
                }
            }
            return false;
        }
        return false;
    }

    public boolean playSound(int typ) {

        switch (typ) {
            case WHAT: {
                return what.get(r.nextInt(what.size())).playSound();
            }
            case CONFIRM: {
                return confirm.get(r.nextInt(confirm.size())).playSound();
            }
            case ATTACK: {
                return attack.get(r.nextInt(attack.size())).playSound();
            }
            case DEATH: {
                return death.get(r.nextInt(death.size())).playSound();
            }
            case BIRTH: {
                return birth.get(r.nextInt(birth.size())).playSound();
            }
            case PISSED: {
                return pissed.get(r.nextInt(pissed.size())).playSound();
            }
        }
        return false;
    }

    public int getUsageforSound(Sound s) {
        if (attack.contains(s)) {
            return ATTACK;
        } else if (birth.contains(s)) {
            return BIRTH;
        } else if (confirm.contains(s)) {
            return CONFIRM;
        } else if (death.contains(s)) {
            return DEATH;
        } else if (pissed.contains(s)) {
            return PISSED;
        } else if (what.contains(s)) {
            return WHAT;
        }

        return NOTINUSE;
    }

    public boolean removeSound(int typ, Sound sound) {
        if (all.remove(sound)) {
            switch (typ) {
                case ATTACK: {
                    return attack.remove(sound);
                }
                case BIRTH: {
                    return birth.remove(sound);
                }
                case CONFIRM: {
                    return confirm.remove(sound);
                }
                case DEATH: {
                    return death.remove(sound);
                }
                case PISSED: {
                    return pissed.remove(sound);
                }
                case WHAT: {
                    return what.remove(sound);
                }
            }
        }
        return false;
    }

    public boolean containsSound(String name) {

        for (Sound sound : attack) {
            if (sound.getEditorName().equals(name)) {
                return true;
            }
        }
        for (Sound sound : birth) {
            if (sound.getEditorName().equals(name)) {
                return true;
            }
        }
        for (Sound sound : confirm) {
            if (sound.getEditorName().equals(name)) {
                return true;
            }
        }
        for (Sound sound : death) {
            if (sound.getEditorName().equals(name)) {
                return true;
            }
        }
        for (Sound sound : pissed) {
            if (sound.getEditorName().equals(name)) {
                return true;
            }
        }

        return false;
    }

    public String getEditorName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return attack.size() + birth.size() + confirm.size() + death.size() + pissed.size() + what.size();
    }

    public Sound get(int i) {
        return all.get(i);
    }

    @Override
    public String toString() {
        return name;
    }

    @Override
    public void setEditorName(String name) {
        this.name = name;
    }
}
